<template>
    <div>
      <el-form label-width="100px" :model="ruleForm" class="demo-ruleForm">
        <el-row>
          <el-col :span="8" :offset="1">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="ruleForm.username" class="custom-input"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="ruleForm.mobile" class="custom-input"></el-input>
            </el-form-item>
            <el-form-item label="电子邮箱" prop="email">
              <el-input v-model="ruleForm.email" class="custom-input"></el-input>
            </el-form-item>
            <el-form-item label="用户地址" prop="address">
              <el-input v-model="ruleForm.address" class="custom-input"></el-input>
            </el-form-item>
            <el-form-item label="用户积分" prop="points">
              <el-input v-model="ruleForm.points" class="custom-input"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm('ruleForm')">立即保存</el-button>
        <el-button @click="$emit('change', false)">取 消</el-button>
      </div>
    </div>
  </template>

<script>
export default {
    data () {
        return {
            ruleForm:JSON.parse(sessionStorage.getItem("EditData"))
        }
    },
    
    props: {
      editData: Object
    },
    model: {
      prop: "dialogFormVisible",
      event: "change"
    },methods: {
        submitForm(){
          if(this.ruleForm.points<50)
            this.ruleForm.membership_level=0;
          else if(this.ruleForm.points>50 && this.ruleForm.points<=100)
          this.ruleForm.membership_level=1;
          else if(this.ruleForm.points>100 && this.ruleForm.points<=150)
          this.ruleForm.membership_level=2;
          else if(this.ruleForm.points>150 && this.ruleForm.points<=200)
          this.ruleForm.membership_level=3;
          else if(this.ruleForm.points>200 && this.ruleForm.points<=250)
          this.ruleForm.membership_level=4;
        else if(this.ruleForm.points>250)
        this.ruleForm.membership_level=5
            this.axios({
                method:"PUT",
                url:"user_info",
                data:this.ruleForm
            })
            .then(res => {
            if (res.data.code > 0) {
              this.$message({
                message: res.data.msg,
                type: "success"
              });
              
              // 关闭弹窗
              this.$emit("change", false);
              // 刷新主页面数据
              this.$emit("refresh");
            } else {
              this.$message({
                message: res.data.msg,
                type: "error"
              });
            }
          });
        }


    },created() {
        if(this.editData){
            this.ruleForm=JSON.parse(JSON.stringify(this.editData))
            
        }
       
    },
    watch: {
        editData: {
            handler(newVal) {
                if (newVal) {
                    this.ruleForm = JSON.parse(JSON.stringify(newVal));
                }
            },
            immediate: true
        }
}}
</script>

<style scoped>
.demo-ruleForm {
  padding: 20px;
  border: 1px solid #e4e7ed;
  border-radius: 5px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.store-form {
    border: 1px solid #e4e7ed;
    border-radius: 4px;
    padding: 20px;
    margin-bottom: 20px;
}
.dialog-footer {
    padding: 10px;
    border-top: 1px solid #e4e7ed;
}
  </style>